<!-- 编辑弹窗 -->
<template>
  <ele-modal :form="true" :destroy-on-close="true" :width="770" v-model="visible" :title="isUpdate ? '修改' : '新增'">
    <el-form ref="formRef" :inline="true" :model="form" :rules="rules" label-width="120px" @submit.prevent="">
      <el-form-item label="上级警告类型" prop="configName" style="width:91%">
        <dict-data code="sys_normal_disable" v-model="form.status" placeholder="请选择" />
      </el-form-item>
      <el-form-item label="类型名称" prop="configKey">
        <el-input clearable v-model="form.configKey" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="类型代码" prop="configValue">
        <el-input clearable v-model="form.configValue" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="告警等级" prop="configName"style="width:40%">
        <dict-data code="sys_normal_disable" v-model="form.status" placeholder="请选择" />
      </el-form-item>
      <el-form-item label="排序号"label-width="146px" prop="orderNum" style="width:47%">
        <el-input-number :min="0" :max="99999" v-model="form.orderNum" placeholder="请输入显示排序" controls-position="right"
          class="ele-fluid" />
      </el-form-item>
      <el-form-item label="告警提示" prop="tips">
        <el-checkbox-group v-model="form.tips">
          <el-checkbox label="语音告警" />
          <el-checkbox label="短信告警" />
          <el-checkbox label="弹框显示" />
          <el-checkbox label="统计显示" />
        </el-checkbox-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
import { ref, reactive, watch } from 'vue';
import { EleMessage } from 'ele-admin-plus';
import { useFormData } from '@/utils/use-form-data';
import { addConfig, updateConfig } from '@/api/system/config';

const props = defineProps({
  /** 修改回显的数据 */
  data: Object
});

const emit = defineEmits(['done']);

/** 弹窗是否打开 */
const visible = defineModel({ type: Boolean });

/** 是否是修改 */
const isUpdate = ref(false);

/** 提交状态 */
const loading = ref(false);

/** 表单实例 */
const formRef = ref(null);

/** 表单数据 */
const [form, resetFields, assignFields] = useFormData({
  configId: void 0,
  configName: '',
  configKey: '',
  configValue: '',
  configType: 'Y',
  tips: ''
});

/** 表单验证规则 */
const rules = reactive({
  configName: [
    {
      required: true,
      message: '请选择',
      type: 'string',
      trigger: 'change'
    }
  ],
  configKey: [
    {
      required: true,
      message: '请输入',
      type: 'string',
      trigger: 'blur'
    }
  ],
  configValue: [
    {
      required: true,
      message: '请输入',
      type: 'string',
      trigger: 'blur'
    }
  ],
  orderNum:[
    {
      required: true,
      message: '请选择',
      type: 'string',
      trigger: 'check'
    }
  ],
  tips:[
    {
      required: true,
      message: '请选择',
      type: 'string',
      trigger: 'check'
    }
  ]
});

/** 关闭弹窗 */
const handleCancel = () => {
  visible.value = false;
};

/** 保存编辑 */
const save = () => {
  formRef.value?.validate?.((valid) => {
    if (!valid) {
      return;
    }
    loading.value = true;
    const saveOrUpdate = isUpdate.value ? updateConfig : addConfig;
    saveOrUpdate(form)
      .then((msg) => {
        loading.value = false;
        EleMessage.success({ message: msg, plain: true });
        handleCancel();
        emit('done');
      })
      .catch((e) => {
        loading.value = false;
        EleMessage.error({ message: e.message, plain: true });
      });
  });
};

/** 监听弹窗打开 */
watch(visible, () => {
  if (visible.value) {
    if (props.data) {
      assignFields(props.data);
      isUpdate.value = true;
    } else {
      resetFields();
      isUpdate.value = false;
    }
  }
});
</script>
